package com.conero.yangsu.controller.system;

import com.conero.yangsu.entity.ModuleEntity;
import com.conero.yangsu.lib.FeedbackData;
import com.conero.yangsu.mapper.ModuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/system/module")
public class ModuleController {
    @Autowired
    private ModuleMapper moduleMapper;

    // 数据列获取
    @PostMapping("/getList")
    public FeedbackData getList(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        Page<ModuleEntity> module = moduleMapper.findAll(PageRequest.of(current - 1, 20));
        HashMap<String, Object> result = new HashMap<>();
        List<ModuleEntity> dataList = module.getContent();
        result.put("data", dataList);

        HashMap<String, Object> pagination = new HashMap<>();
        pagination.put("total", module.getNumber());
        pagination.put("current", current);
        result.put("pagination", pagination);
        return FeedbackData.success(result, "数据请求成功！");
    }

    //数据新增
    @PostMapping("/add")
    public FeedbackData add(ModuleEntity moduleEntity) {
        moduleMapper.save(moduleEntity);
        return FeedbackData.success("数据新增成功！");
    }

    //数据新增
    @PostMapping("/upd")
    public FeedbackData upd(ModuleEntity moduleEntity) {
        moduleMapper.save(moduleEntity);
        return FeedbackData.success("数据更新成功！");
    }

    //数据新增
    @PostMapping("/del")
    public FeedbackData del(ModuleEntity moduleEntity) {
        if (moduleEntity.getId() == null) {
            return FeedbackData.error("数据删除失败，缺少必要参数！");
        }
        moduleMapper.deleteById(moduleEntity.getId());
        return FeedbackData.success("数据更新成功！");
    }
}
